////////////////////////////////////////////////
// -- DepthHUD Lite                           //
// by Hurricaaane (Ha3)                       //
//                                            //
// http://www.youtube.com/user/Hurricaaane    //
//--------------------------------------------//
// Menu                                       //
////////////////////////////////////////////////
local dhlite = dhlite

local DHLITE_MENU = nil

function dhlite:GetMenu()
	return DHLITE_MENU or self:BuildMenu()
	
end

function dhlite:UpdateMenuPosition()
	//local pos = self:GetVar( "menu_position" )
	/*if pos > 0 then
		DHLITE_MENU:SetPos( ScrW() - DHLITE_MENU:GetWide(), (ScrH() - DHLITE_MENU:GetTall()) / 2 )
		DHLITE_MENU:GetContents()._p_topPanel._p_positionBox:SetType( "left" )
		
	else
		DHLITE_MENU:SetPos( 0, (ScrH() - DHLITE_MENU:GetTall()) / 2 )
		DHLITE_MENU:GetContents()._p_topPanel._p_positionBox:SetType( "right" )
		
	end*/
	
	DHLITE_MENU:SetPos( (ScrW() - DHLITE_MENU:GetWide()) / 2, (ScrH() - DHLITE_MENU:GetTall()) / 2 )
	
end

function dhlite:BuildMenuContainer()
	self:RemoveMenu()
	
	DHLITE_MENU = vgui.Create( DHLITE_SHORT .. "_ContextContainer" )
	DHLITE_MENU.DEF_WIDTH = 230
	DHLITE_MENU.DEF_TALL = ((ScrH() * 0.8) > (ScrH() - 200)) and (ScrH() * 0.8) or (ScrH() - 200)
	DHLITE_MENU:SetSize( DHLITE_MENU.DEF_WIDTH, DHLITE_MENU.DEF_TALL )
	DHLITE_MENU:GetCanvas( ):SetDrawBackground( false )
	
	local mainPanel = vgui.Create( "DPanel" )
	DHLITE_MENU:SetContents( mainPanel )
	
	///
	
	//mainPanel:SetDrawBackground( false )
	/*mainPanel.Paint = function (self)
		surface.SetDrawColor( 255, 0, 0, 96 )
		surface.DrawRect( 0, 0, self:GetWide(), self:GetTall() )
	end*/
	
	DHLITE_MENU.Paint = function (self)
		surface.SetDrawColor( 0, 0, 0, 96 )
		surface.DrawRect( 0, 0, self:GetWide(), self:GetTall() )
	end
	
	mainPanel.Paint = function (self)
		surface.SetDrawColor( 0, 0, 0, 96 )
		surface.DrawRect( 0, 0, self:GetWide(), self:GetTall() )
	end
	
	return DHLITE_MENU
	
end

function dhlite:BuildMenu()
	if not ValidPanel( DHLITE_MENU ) then
		self:BuildMenuContainer()
		
	end
	
	local mainPanel = DHLITE_MENU:GetContents()
	
	////
	local topPanel = dhlite:BuildHeader( mainPanel, DHLITE_NAME )
	
	////
	local tabMaster = vgui.Create( "DPropertySheet", mainPanel )
	do
		local formItems = vgui.Create("DPanelList")
		local itemMaster = vgui.Create("DPanel")
		itemMaster:SetText( "Drag the elements here in and out")
		itemMaster:SetTall( 300 )
		formItems:AddItem( itemMaster )
		itemMaster.Think = function( self )			
			if dhlite:IsMoveMode() then
				//local mx, my = self:CursorPos()
				local referencePanel = DHLITE_MENU
				local mx, my = referencePanel:CursorPos()
				local isInPanel = (mx > 0)
						and (my > 0)
						and (mx < referencePanel:GetWide())
						and (my < referencePanel:GetTall())
				print( isInPanel )
				chat.AddText( Color(255, 0, 0), tostring( isInPanel ) )
				local movePanel = dhlite:GetMoveModePanel()
				if ValidPanel( movePanel ) then
					if movePanel:IsStored() and not isInPanel then
						movePanel:SetParent( dhlite:GetCanvas() )
						movePanel:SetCanvas( dhlite:GetCanvas() )
						movePanel:SetStore( false )
						
						movePanel:SetVirtualGridPos( movePanel:EvaluateVirtualGridPos( dhlite:GetVar( "editor_snap" ), dhlite._iMoveX, dhlite._iMoveY ) )
						movePanel:ApplyVirtualGridPos( )
						surface.PlaySound( "dhlite/activate_off.wav" )
					
					elseif not movePanel:IsStored() and isInPanel then
						movePanel:SetParent( self )
						movePanel:SetCanvas( self )
						movePanel:SetStore( true )
						
						movePanel:SetVirtualGridPos( movePanel:EvaluateVirtualGridPos( dhlite:GetVar( "editor_snap" ), dhlite._iMoveX, dhlite._iMoveY ) )
						movePanel:ApplyVirtualGridPos( )
						surface.PlaySound( "dhlite/activate_on.wav" )
						
					end
					
				end
				
			end
			
		end
		
		local formScripts = vgui.Create( "DPanelList" )
		do
			formScripts:AddItem( self:BuildParamPanel( "core_debug", { Type = "bool", Text = "Debug Mode" } ) )
			formScripts:AddItem( self:BuildParamPanel( "editor_snap", { Type = "range", Text = "Grid snap", Min = 16, Max = 32 } ) )
			
		end
		
		tabMaster:AddSheet( "Items", formItems, "gui/silkicons/application_view_detail", false, false, "All your scripts." )
		tabMaster:AddSheet( "Options", formScripts, "gui/silkicons/wrench", false, false, "Inscript Settings." )
		
	end
	
	////
	////
	local optionsForm = vgui.Create( "DForm", mainPanel )
	do
		optionsForm:SetAutoSize( true )
		optionsForm:SetName( "Edit mode" )
				
		optionsForm.BuildLabel = function()
			local label = sharpeye:BuildParamPanel( "noconvars", { Type = "panel_label", Text = "", Wrap = true } )
			label:SetText("DepthHUD Lite is in edit mode while the menu is open. You can use your mouse to move things around to edit them.")
			label:InvalidateLayout( true )
			label:InvalidateParent( true )
			
			return label
		end
		
		optionsForm:AddItem( optionsForm:BuildLabel() )
		optionsForm._p_label = label
		optionsForm.NormalMode = function (self)
			self:Clear()
			self:AddItem( optionsForm:BuildLabel() )
			self:Rebuild()
			self:InvalidateLayout( true )
			self:InvalidateParent( true )
			
		end	
		
	end

	
	////
	mainPanel._p_topPanel = topPanel
	mainPanel._p_tabMaster = tabMaster
	mainPanel._p_optionsForm = optionsForm
	
	mainPanel._n_Spacing = 5
	mainPanel.PerformLayout = function (self)
		self:GetParent():StretchToParent( 0, 0, 0, 0 )
		self:StretchToParent( self._n_Spacing, self._n_Spacing, self._n_Spacing, self._n_Spacing )
		self._p_topPanel:PerformLayout()
		self._p_tabMaster:PerformLayout()
		self._p_optionsForm:PerformLayout()
		self._p_topPanel:Dock( TOP )
		self._p_optionsForm:Dock( BOTTOM )
		self._p_tabMaster:Dock( FILL )
	end
	
	DHLITE_MENU:UpdateContents()
	self:UpdateMenuPosition()
	
	return DHLITE_MENU
	
end

function dhlite:RemoveMenu()
	if ValidPanel( DHLITE_MENU ) then
		DHLITE_MENU:Remove()
		DHLITE_MENU = nil
	
	end
	
end

function dhlite:MountMenu()
	self:CreateVarParam( "bool", "menu_position", "1", { callback = function ( a, b, c ) dhlite:UpdateMenuPosition() end } )
	-- do nothing
	
end

function dhlite:UnmountMenu()
	self:RemoveMenu()
	
end

function dhlite:OpenMenu()
	self:GetMenu():Open()
	self:SetCanvasEditable( true )
	
end

function dhlite:CloseMenu()
	self:GetMenu():Close()
	self:SetCanvasEditable( false )
	
end



function dhlite:BuildHeader( mainPanel, sHeaderName )
	////
	local topPanel = vgui.Create( "DPanel", mainPanel )
	do
		local title = self:BuildParamPanel( "noconvar", { Type = "panel_label", Text = sHeaderName, ContentAlignment = 5, Font = "DefaultBold" } )
		title.Paint = function (self)
			surface.SetDrawColor( 0, 0, 0, 96 )
			surface.DrawRect( 0, 0, self:GetWide(), self:GetTall() )
		end
		title:SetParent( topPanel )
		title.OnMousePressed = function ( self )
			self.mx, self.my = self:CursorPos()
			self._moving = true
			self:MouseCapture( true )
		end
		title.OnMouseReleased = function ( self )
			self._moving = false
			self:MouseCapture( false )
		end
		title.Think = function ( self )
			if not self._moving then return end
			
			local rx, ry = gui.MousePos()
			local realx, realy = rx - self.mx, ry - self.my
			local w, h = DHLITE_MENU:GetWide(), DHLITE_MENU:GetTall()
			
			if realx < 0 then
				realx = 0
				realy = 0
				DHLITE_MENU:SetTall( ScrH() )
				DHLITE_MENU._largemode = true
				
			elseif (realx + w) > ScrW() then
				realx = ScrW() - w
				realy = 0
				DHLITE_MENU:SetTall( ScrH() )
				DHLITE_MENU._largemode = true
				
			else
				if DHLITE_MENU._largemode then
					DHLITE_MENU._largemode = false
					DHLITE_MENU:SetTall( DHLITE_MENU.DEF_TALL )
					
				end
				
				if realy < 0 then realy = 0 end
				if (realy + h) > ScrH() then realy = ScrH() - h end
				
			end
			
			DHLITE_MENU:SetPos( realx, realy )
			
		end
		
		local subTitle = nil
		do
			local MY_VERSION, ONLINE_VERSION = dhlite_internal.GetVersionData()
			MY_VERSION = "v" .. tostring(MY_VERSION)
			ONLINE_VERSION = (ONLINE_VERSION == -1) and "(?)" or ("v" .. tostring( ONLINE_VERSION ))
			subTitle = self:BuildParamPanel( "noconvar", { Type = "panel_label", Text = "Using " .. (dhlite_cloud:IsUsingCloud() and "Cloud " .. ONLINE_VERSION or "Locale " .. MY_VERSION), ContentAlignment = 4 } )
		end
		subTitle:SetParent( topPanel )
		
		local MY_VERSION, ONLINE_VERSION = dhlite_internal.GetVersionData()
		if ((MY_VERSION < ONLINE_VERSION) and dhlite_cloud:IsUsingCloud()) then
			subTitle:SetToolTip( "There is an update ! You're currently using a temporary copy of the new version (You have v" .. tostring( MY_VERSION ) .. " installed)." )
			subTitle.Think = function (self)
				local blink = 127 + (math.sin( math.pi * CurTime() * 0.5 ) + 1 ) * 64
				self:SetColor( Color( 255, 255, 255, blink ) ) // TODO : ?
				
			end
			
		end
		
		local enableBox = self:BuildParamPanel( "core_enable", { Type = "bool_nolabel", Style = "grip" } )
		enableBox:SetParent( title )
		enableBox:SetToolTip( "Toggle " .. tostring( sHeaderName ) .. "." )
		enableBox.Paint = function (self)
			local isEnabled = self:GetChecked()
			if isEnabled then
				--local blink = (math.sin( math.pi * CurTime() ) + 1 ) / 2 * 64
				local blink = 222 + (math.sin( math.pi * CurTime() ) + 1 ) * 16
				surface.SetDrawColor( blink, blink, blink, 255 )
				
			else
				surface.SetDrawColor( 192, 192, 192, 255 )
				
			end
			
			surface.DrawRect( 0, 0, self:GetWide(), self:GetTall() )
			
			surface.SetDrawColor( 0, 0, 0, 255 )
			surface.DrawOutlinedRect( 0, 0, self:GetWide(), self:GetTall() )
			
			if not isEnabled and ( CurTime() % 1 > 0.5 ) then
				surface.DrawOutlinedRect( 2, 2, self:GetWide() - 4, self:GetTall() - 4 )
				
			end
			
		end
		
		local closeBox = self:BuildParamPanel( "noconvar", { Type = "panel_sysbutton", Style = "close", DoClick = function ( self ) dhlite:CallCmd("-menu") end } )
		closeBox:SetParent( title )
		
		/*local positionBox = self:BuildParamPanel( "noconvar", { Type = "panel_sysbutton", Style = "left", DoClick = function ( self ) dhlite:SetVar( "menu_position", (dhlite:GetVar( "menu_position" ) > 0) and 0 or 1 ) end } )
		positionBox:SetParent( title )
		positionBox:SetToolTip( "Change menu dock position." )*/
		
		local reloadCloud = self:BuildParamPanel( "noconvar", { Type = "panel_imagebutton", Material = "gui/silkicons/toybox", DoClick = function() dhlite:CallCmd("-menu") dhlite:ReloadFromCloud() end } )
		reloadCloud:SetParent( subTitle )
		reloadCloud:SetToolTip( "Press to use the latest version from the Cloud." )
		
		local reloadLocale = self:BuildParamPanel( "noconvar", { Type = "panel_imagebutton", Material = "gui/silkicons/application_put", DoClick = function() dhlite:CallCmd("-menu") dhlite:ReloadFromLocale() end } )
		reloadLocale:SetParent( subTitle )
		reloadLocale:SetToolTip( "Press to use your Locale installed version." )
		
		local loadChangelog = self:BuildParamPanel( "noconvar", { Type = "panel_button", Text = "Changelog", DoClick = function() dhlite:CallCmd("call_changelog") end } )
		loadChangelog:SetParent( subTitle )
		loadChangelog:SetToolTip( "Press to view the changelog." )
		
		if MY_VERSION < ONLINE_VERSION then
			loadChangelog.PaintOver = function ( self )
				local blink = (math.sin( math.pi * CurTime() * 0.5 ) + 1 ) * 64
				surface.SetDrawColor( 255, 255, 255, blink )
				draw.RoundedBoxEx( 2, 0, 0, self:GetWide(), self:GetTall(), Color( 255, 255, 255, blink ), true, true, true, true  )
				
			end
			loadChangelog:SetToolTip( "There are updates ! You should update your Locale." )
			
		else
			loadChangelog:SetToolTip( "Press to view the changelog." )
			
		end
		
		
		topPanel._p_title = title
		topPanel._p_subTitle = subTitle
		topPanel._p_enableBox = enableBox
		topPanel._p_closeBox = closeBox
		topPanel._p_positionBox = positionBox
		
		topPanel._p_reloadCloud = reloadCloud
		topPanel._p_reloadLocale = reloadLocale
		topPanel._p_loadChangelog = loadChangelog
	
	end
	topPanel.Paint = function (self)
		surface.SetDrawColor( 0, 0, 0, 96 )
		surface.DrawRect( 0, 0, self:GetWide(), self:GetTall() )
	end
	topPanel.PerformLayout = function (self)
		self:SetWide( self:GetParent():GetWide() )
		self._p_title:SetWide( self:GetWide() )
		self._p_subTitle:SetWide( self:GetWide() )
		
		self._p_title:PerformLayout( )
		self._p_subTitle:PerformLayout( )
		self._p_enableBox:PerformLayout( )
		//self._p_positionBox:PerformLayout( )
		self._p_closeBox:PerformLayout( )
		
		self._p_reloadCloud:PerformLayout( )
		self._p_reloadLocale:PerformLayout( )
		self._p_loadChangelog:PerformLayout( )
		
		self._p_title:CenterHorizontal( )
		self._p_subTitle:CenterHorizontal( )
		
		self:SetTall( self._p_title:GetTall() + self._p_subTitle:GetTall() )
		
		self._p_title:AlignTop( 0 )
		self._p_subTitle:SetWide( self._p_subTitle:GetWide() - 4 )
		self._p_subTitle:AlignLeft( 4 )
		self._p_subTitle:MoveBelow( self._p_title, 0 )
		
		local boxSize = self._p_title:GetTall()
		self._p_enableBox:SetSize( boxSize * 0.8, boxSize * 0.8 )
		//self._p_positionBox:SetSize( boxSize * 0.8, boxSize * 0.8 )
		self._p_closeBox:SetSize( boxSize * 0.8, boxSize * 0.8 )
		self._p_enableBox:CenterVertical( )
		//self._p_positionBox:CenterVertical( )
		self._p_closeBox:CenterVertical( )
		self._p_enableBox:AlignLeft( boxSize * 0.1 )
		self._p_closeBox:AlignRight( boxSize * 0.1 )
		//self._p_positionBox:MoveLeftOf( self._p_closeBox, boxSize * 0.1 )
		
		local buttonSize = self._p_subTitle:GetTall()
		self._p_reloadCloud:SetSize( buttonSize * 0.8, buttonSize * 0.8 )
		self._p_reloadLocale:SetSize( buttonSize * 0.8, buttonSize * 0.8 )
		self._p_loadChangelog:SizeToContents( )
		self._p_loadChangelog:SetSize( self._p_loadChangelog:GetWide() + 6, buttonSize * 0.8 )
		self._p_reloadCloud:CenterVertical( )
		self._p_reloadLocale:CenterVertical( )
		self._p_loadChangelog:CenterVertical( )
		self._p_reloadCloud:AlignRight( boxSize * 0.1 )
		self._p_reloadLocale:MoveLeftOf( self._p_reloadCloud, boxSize * 0.1 )
		self._p_loadChangelog:MoveLeftOf( self._p_reloadLocale, boxSize * 0.3 )
	end
	
	return topPanel
	
end
